from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models
from rest_framework import status
from django.contrib.auth.hashers import make_password,check_password
from .serializer import *
from django.core.paginator import Paginator


# 注册
class ResgisterView(APIView):
    def post(self,request):
        # 1.接收参数
        username=request.data.get('username','').strip()
        password=request.data.get('password','').strip()

        # 2.验证数据的重复性
        userobj=models.User.objects.filter(username=username).first()
        if userobj:
            return Response('数据已存在',status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # 3.构造数据
        pwd=make_password(password)
        data={
            'username':username,
            'password':pwd
        }

        # 反序列化
        obj=UserSerializer(data=data)
        if obj.is_valid():
            obj.save()
            return Response('注册成功',status=status.HTTP_200_OK)

        # 打印信息
        print(obj.errors)

        # 返回
        return Response('参数有误', status=status.HTTP_500_INTERNAL_SERVER_ERROR)


# 登录
class LoginView(APIView):
    def post(self,request):
        # 1.接收参数
        username=request.data.get('username','').strip()
        password = request.data.get('password', '').strip()

        # 验证用户名是否存在
        userobj=models.User.objects.filter(username=username).first()

        if not userobj:
            return Response('用户名不存在', status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # 获取数据库中的密码
        pwd=userobj.password

        if check_password(password,pwd):
            return Response('登录成功')
        return Response('用户名或密码错误', status=status.HTTP_500_INTERNAL_SERVER_ERROR)



# 专业
class ProfessionalView(APIView):
    def get(self,request):
        # 1.获取前端得数据
        p=request.GTE.get('p',1)

        # 2.查询所有数据
        zhuanyeobj=models.Professoinal.objects.all()

        # 分页
        page_obj=Paginator(zhuanyeobj,per_page=2)

        # 范围
        page=[i for i in page_obj.page_range]

        # 当前页得数据
        page_data=page_obj.page(p)

        # 上一页和下一页
        previous=page_data.has_previous()
        next=page_data.has_next()

        # 序列化当前数据
        obj=ProfessionalSerializer(page_data,many=True)

        # 构造数据
        data={
            'data':obj.data,
            'page':page,
            'previous':previous,
            'next':next
        }
        return Response(data)


    def post(self,request):
        # 1.接收参数
        name=request.data.get('name','').strip()

        # 2.验证数据得一致性
        zhuanyeobj=models.Professoinal.objects.filter(name=name).first()
        if zhuanyeobj:
            return Response('数据已存在',status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # 3.构造数据
        data={
            'name':name
        }

        # 序列化
        obj=ProfessionalSerializer(data=data)

        if obj.is_valid():
            obj.save()
            return Response('添加成功', status=status.HTTP_200_OK)

        print(obj.errors)

        return Response('参数有误', status=status.HTTP_500_INTERNAL_SERVER_ERROR)










